package com.hpe.controller;

import com.hpe.model.Product;
import com.hpe.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by tiansha on 2015/12/2.
 */
@Controller
public class ProductController {

    @Autowired
    private ProductRepository productRepository;

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true)); //yyyy-MM-dd HH:mm:ss
    }

    @RequestMapping(value = "/products", method = RequestMethod.GET)
    public String list(Model model, Pageable pageable){
        Iterable<Product> products = productRepository.findAll();
        model.addAttribute("products", products);
        return "products";
    }

    @RequestMapping(value = "/products2", method = RequestMethod.GET)
    public String page(Model model, Pageable pageable){
        Page<Product> productPage = productRepository.findAll(pageable);
        PageWrapper<Product> page = new PageWrapper<Product>(productPage, "/products2");
        model.addAttribute("products", page.getContent());
        model.addAttribute("page", page);
        return "products2";
    }

    @RequestMapping("product/{id}")
    public String showProduct(@PathVariable("id") Product product, Model model){
        model.addAttribute("product", product);
        return "productshow";
    }

    @RequestMapping("product/edit/{id}")
    public String edit(@PathVariable("id") Product product, Model model){
        model.addAttribute("product", product);
        return "productform";
    }

    @RequestMapping("product/new")
    public String newProduct(Model model){
        model.addAttribute("product", new Product());
        return "productform";
    }

    @RequestMapping(value = "product", method = RequestMethod.POST)
    public String saveProduct(@Valid Product product, final BindingResult result, RedirectAttributes redirect){
        // check name unique
        if (product.getId() == null && productRepository.findByName(product.getName()) != null) {
            result.rejectValue("name", "product.name.unique");
        }

        if (result.hasErrors()) {
            return "productform";
        }
        productRepository.save(product);
        redirect.addFlashAttribute("globalMessage", "Successfully created a new product");
        return "redirect:/product/" + product.getId();
    }

    @RequestMapping("product/delete/{id}")
    public String delete(@PathVariable("id") Long id, RedirectAttributes redirect){
        productRepository.delete(id);
        redirect.addFlashAttribute("globalMessage", "Successfully deleted a new product");
        return "redirect:/products2";
    }

}
